/*
* Arduino Wireless Communication Tutorial
*       Example 1 - Receiver Code
*                
* by Dejan Nedelkovski, www.HowToMechatronics.com
* 
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

//nrf24l 接线  GND--GND VCC--3.3V
//SCK--13  MISO--12 MOSI--11 CSN--8 CE--7
//io  ledio-9 and 10 继电器--2 
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5,6); // RX, TX
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct Data_Package {
  byte X;
  byte Y;
  byte G;
  byte H;
};
Data_Package data;
int  xa, ya;
int  ga, ha;
int xaa,xbb;
int yaa,ybb;
int n;
unsigned long lastReceiveTime = 0;
void setup() {
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  pinMode(2,HIGH);
  digitalWrite(2,HIGH);
  pinMode(9,OUTPUT);
  digitalWrite(9,LOW);
  mySerial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {

  if(radio.available()) {
//    char text[64] = "";
//    radio.read(&text, sizeof(text));
//    Serial.println(text);
      radio.read(&data, sizeof(Data_Package));
      lastReceiveTime = millis();
      xa = data.X;
      ya = data.Y;
      ga = data.G;
      ha = data.H;
      if(ha==7)digitalWrite(9,HIGH);else digitalWrite(9,LOW);//led1
      if(ha==8)digitalWrite(10,HIGH);else digitalWrite(10,LOW);//led2
      if(ga==7)digitalWrite(2,LOW);else digitalWrite(2,HIGH);//继电器低电平  公共端 和 常开 连接
      Serial.println("x and y:  cmdA and cmdB");
      Serial.print(xa);Serial.print(" , ");Serial.print(ya);Serial.print(" ; ");
      Serial.print(ga);Serial.print(" , ");Serial.println(ha);
      n=1;
  }
  PS2();
//  if (ya < 110 and n==1) {
//    // Convert the declining Y-axis readings for going backward from 110 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
//    yaa= map(ya, 110, 0, 0, 255);
//    ybb= map(ya, 110, 0, 0, 255);
//   Serial.println(ybb); Serial.println(yaa);
//   n=0;
//  }
  delay(100);
//  delay(400);
//  Serial.println("23");
}


void PS2()

{
  if(xa==91 and n==1 and ya==91)
  {Serial.println("0");
  mySerial.println("0");
  }
  
    //Forward and Backward
   if (xa < 89 and n==1 and 5<ya<240) 
   {
    // Convert the declining Y-axis readings for going backward from 110 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    xaa= map(xa, 91, 0, 0, 255);
    xbb= map(xa, 91, 0, 0, 255);
//    Serial.print("x up:");Serial.println(xaa);
   Serial.println("1");
   mySerial.println("1");
   }else if (xa > 95 and n==1 and 5<ya<240)
   {
    xaa= map(xa, 95, 255, 0, 255);
    xbb= map(xa, 95, 255, 0, 255);
//    Serial.print("x down:");Serial.println(xaa);
   Serial.println("2");
   mySerial.println("2");
   }
    //Left and Right
   if (ya < 89 and n==1 and 5<=xa<255) 
   {
    // Convert the declining Y-axis readings for going backward from 110 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    yaa= map(ya, 89, 0, 0, 255);
    ybb= map(ya, 89, 0, 0, 255);
//   Serial.print("y right:");Serial.println(yaa);
    Serial.println("3");
    mySerial.println("3");
//   n=0;
   }else if (ya > 95 and n==1 and 5<=xa<255)
   {
    yaa= map(ya, 95, 255, 0, 255);
    ybb= map(ya, 95, 255, 0, 255);
//    Serial.print("y left:");Serial.println(yaa);
    Serial.println("4");
    mySerial.println("4");
   }
    n=0;
}
